import json as js
import os.path
import shutil
import subprocess
import time
import tkinter.messagebox
from sys import argv
import _AES
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

# path = "D:\\test\\"
path = os.getcwd() + "\\"
file = "a.b"
password = "password.b"
daan = "123456cat"
is_delete = False


def init():
    print(os.getcwd())
    global is_delete
    # 获取当前时间
    time_now = int(time.time())
    # 创建文件
    if not os.path.exists(path + file):
        print("创建文件a.b")
        with open(path + file, "w+") as c:
            pass
    if not os.path.exists(path + password):
        print("创建文件password")
        with open(path + password, "w+") as c:
            pass
    # 读取日期文件
    with open(path + "a.b", "rb+") as f:
        data = f.read().decode('utf-8')
        # 日期文件不为空
        if data != "":
            print("日期文件不为空")
            try:
                data = js.loads(_AES.decrypt(data))
                print(data)
            except:
                # 文件被改动
                print("错误")
                is_delete = True
                return
            # 如果times大于等于1，说明间隔3天后登陆者不是本人，在下次开启后直接开删
            if data['times'] >= 1:
                with open(path + password, "r+") as q:
                    pw = q.read()
                    # 密码对不上
                    if pw != daan:
                        # 删除文件
                        print("开删")
                        is_delete = True
                        return
                    else:
                        q.seek(0)
                        q.truncate()
                        f.seek(0)
                        f.truncate()
                        data = {'date': time_now, 'times': 0}
                        f.write(_AES.encrypt(js.dumps(data)))
                        return
            # 日期和上次登录相隔有3天，times+1
            if time_interval(time_now, data['date']) > 3:
                print("间隔大于3天")
                data['times'] += 1
                tkinter.messagebox.showinfo('提示', '不清楚可以无视')
            f.seek(0)
            f.truncate()
            data = {'date': time_now, 'times': data['times']}
            f.write(_AES.encrypt(js.dumps(data)))
        else:
            data = {'date': time_now, 'times': 0}
            f.write(_AES.encrypt(js.dumps(data)))


# 获取时间差，单位为天
def time_interval(time1, time2):
    return abs((time1 - time2) / 60 / 60 / 24)


# 删除目录下所有东西
def delete_all(path):
    for root, dirs, files in os.walk(path):
        for dir in dirs:
            shutil.rmtree(path + dir)
        if os.path.exists("程序.exe"):
            files.remove("程序.exe")
        for file in files:
            os.remove(path + file)
    subprocess.Popen(f"WAITFOR /T 2 PAUSE 2>NUL & DEL \"{argv[0]}\" /f", shell=True, creationflags=0x00000008)


def start_delete():
    if is_delete:
        delete_all(path)


if __name__ == '__main__':
    # e = test.encrypt("hello     world")  # 加密
    # d = test.decrypt(e)  # 解密
    # print("加密:", e)
    # print("解密:", d)
    init()
    start_delete()
